<template>
	<view class="ts-column ts-flex-item">
		<ts-banner :image="config.cover_image" height="250"></ts-banner>
		<view class="ts-row">
			<view class="ts-row ts-flex-item">
				<ts-button type="primary">全部分类</ts-button>
			</view>
			<view class="ts-row ts-flex-item">
				<ts-button type="success">全部分类</ts-button>
			</view>
			<view class="ts-row ts-flex-item">
				<ts-button type="danger">全部分类</ts-button>
			</view>
		</view>
		<ts-search-bar v-model="keywords" @search="doSearch"></ts-search-bar>
		<view class="ts-gap"></view>

		<view class="ts-column ts-flex-item">
			<view v-for="(item,index) in list" :key="index" class="ts-column ts-flex-item">
				<view class="ts-column ts-padding">
					<image :src="item.cover_image" mode="" style="width: 100%; height: 250upx;"></image>
					<view class="ts-row">
						<text class="ts-h4">{{item.title}}</text>
					</view>
					<view class="ts-row">
						<text>{{item.service}}</text>
					</view>
					<view class="ts-row">
						<text>{{item.address}}</text>
					</view>
				</view>
				<view class="ts-gap"></view>
			</view>
		</view>
		<view class="ts-column">
			<ts-load-more :loadingType="loadingType" :contentText="contentText" @tap="getStoreListMoreData"></ts-load-more>
		</view>
	</view>
</template>

<script>
	import tsBanner from 'components/teaset/components/ts-banner.vue';
	import tsSearchBar from 'components/teaset/components/ts-search-bar.vue';

	var self;
	export default {
		components: {
			tsBanner, //ts-banner
			tsSearchBar, //ts-search-bar
		},
		data: {
			keywords: '',
			page: 0,
			list: [],
			config: {},
			loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
		},
		onLoad() {
			self = this;
			this.getAppConfig();
			this.getStoreListMoreData();
		},
		onReachBottom() {
			this.getStoreListMoreData();
		},
		methods: {
			getAppConfig() {
				const api = this.$API_BASE + '/app/store/getAppConfig';

				uni.request({
					url: api,
					method: 'GET',
					dataType: 'json',
					data: {
						page: self.page,
					},
					success(response) {
						const res = response.data;
						if (res.success) {
							const data = res.data;
							if (data) {
								self.config = data;
							}
						} else {
							console.log(res.message);
						}
					}
				})
			},
			getStoreListMoreData() {
				const api = this.$API_BASE + '/app/store/getStoreList';
				
				if (this.loadingType !=0){
					return;
				}
				self.page = self.page+1;
				self.loadingType =1;

				uni.request({
					url: api,
					method: 'GET',
					dataType: 'json',
					data: {
						page: self.page,
					},
					success(response) {
						// console.log(JSON.stringify(response));

						const res = response.data;
						//res数据格式
						// 						{
						// 							success:true,
						// 							data:[],
						// 							has_more:true,
						// 							page:1,
						// 						}

						if (res.success) {
							const data = res.data;
							// console.log(JSON.stringify(res));
							if (data) {
								self.list = self.list.concat(data);
							}
							
							if (res.has_more){
								self.loadingType=0; //开启新一轮加载
							}else{
								self.loadingType=2;//没有更多数据了
							}
						} else {
							console.log(res.message);
						}
					}
				})
			},

			doSearch() {
				//
			},
		}
	}
</script>

<style>
	page view {
		/* font-size: 32upx;
	display: flex;
	flex-direction: row; */

		/* border: #000000 solid 1px; */
		/* margin: 10upx; */
	}

	.ts-column {
		display: flex;
		flex-direction: column;
	}

	.ts-flex-item {
		flex: 1;
	}
</style>
